const db = require("../../module/db/db.js")
const fs = require('fs')
const jsonParse = require('../../lib/jsonParse.js')
const axios = require("../../lib/params.js")

const phoneInfo = db.getSchema("phoneInfo") // 手机
const notebookInfo = db.getSchema("notebookInfo") // 笔记本
const makeupInfo = db.getSchema("makeupInfo") // 美妆
const phoneDetail = db.getSchema("phoneDetail") // 手机详情

module.exports = {
    phoneInfo,
    notebookInfo,
    makeupInfo,
    // 获取列表
    async getGoods(request, response, handler) {
        let params = await axios.grabPostParams(request)
        let body = params.name ? {
            goodsType: params.type,
            goodsTitle: new RegExp(params.name),
        } : {
            goodsType: params.type,
        }
        if(params.delFlag == 0) {
            body.delFlag = 0
        }
        let dbFn = phoneInfo
        if (params.type == 1) {
            dbFn = phoneInfo
        } else if (params.type == 2) {
            dbFn = notebookInfo
        } else if (params.type == 3) {
            dbFn = makeupInfo
        }
        dbFn.find(body).exec(function (err, data) {
            // if(data.length){
            jsonParse.sendResult(response, 200, data, '查询成功')
            // } 
            // if(!data.length && dbFn == 'phoneInfo'){
            //     phoneInfo.insertMany(text).then(function(err,data){
            //         jsonParse.sendResult(response, 200, '添加成功', )
            //     })
            // }
        })
    },
    // 获取物品详情
    async getGoodsDetail(request, response, handler) {
        let params = await axios.grabPostParams(request)
        
        console.log(params, 88888888)
        let body = {
            goodsCode: params.goodsCode
        }
        phoneDetail.find(body).exec(function (err, data) {
            jsonParse.sendResult(response, 200, data, '查询成功')
        })
    },
    // 更新物品状态
    async updateStatus(request, response, userName) {
        let params = await axios.grabPostParams(request)
        let dbFn = phoneInfo
        if(params.type == 1) {
            dbFn = phoneInfo
        } else if(params.type == 2) {
            dbFn = notebookInfo
        } else if(params.type == 3) {
            dbFn = makeupInfo
        }
        console.log(params)
        phoneInfo.updateMany({
            goodsCode: {
                $in: params.codes
            }
        }, {
            $set: {
                delFlag: params.status
            }
        }).then(res => {
            console.log(res)
            jsonParse.sendResult(response, 200, {}, `更新成功${res.ok}条，更新失败${res.n}条`)
        })
    },
    // 添加商品
    async addGoods(){

    }
}